
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css"/>
    <script type="text/javascript" src="${pageContext.request.contextPath}/layui/layui.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<%--修改--%>
<div id="goodsEmp" style="display: none;padding: 10px">
    <form class="layui-form" lay-filter="goodsForm">
        <input type="text" style="display: none" name="deptid" autocomplete="off"  class="layui-input"/>
        <div class="layui-form-item">
            <label class="layui-form-label">部门名称:</label>
            <div class="layui-input-block">
                <input type="text" name="deptname"  lay-verify="required" autocomplete="off" placeholder="部门名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门描述:</label>
            <div class="layui-input-block">
                <input type="text" name="remark"  lay-verify="required" autocomplete="off" placeholder="部门描述" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="update">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<table class="layui-hide" id="test" lay-filter="test"></table>

<div id="dem" hidden>
    <table id="demo01" style="display: none" lay-filter="dem"></table>
</div>
<%--头部工具栏--%>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="sxin">刷新</button>
        <button class="layui-btn layui-btn-sm" lay-event="addFood">增加</button>
    </div>
</script>

<%--监听工具栏--%>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="sel">查询该部门对应的员工</a>
</script>
<table class="layui-hide" id="test" lay-filter="test"></table>
<body>
<script>
    layui.use(['table','form','jquery','layer','upload','element'], function() {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;
        var element = layui.element;
        var layer = layui.layer;
        table.render({
            elem: '#test'
            // ,id:"test"
            , url: '${pageContext.request.contextPath}/dept/listByCondition'
            , toolbar: '#toolbar' //开启头部工具栏，并为其绑定左侧模板
            , title: '员工数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'deptid', title: '编号',  sort: true}
                ,{field: 'deptname', title: '部门名' }
                ,{field: 'remark', title: '部门描述',}
                ,{field: 'counts', title: '人数' }
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 300}
            ]]
            , page: true
            , limit: 5 //默认每页显示5条
            , limits:[2,5,10,15]
        });
        //监听工具栏
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                var flag=1;
                if(data.counts>0){
                    flag = 0;
                    layer.msg("该部门有员工存在，不能删除")
                }
                layer.confirm('真的删除行吗', function(index){
                    $.get("${pageContext.request.contextPath}/dept/delete",{"deptid":data.deptid},function (result) {
                        if (result.code===2000){
                            layer.closeAll();
                            layer.msg(result.msg,{icon:1});
                            table.reload('test')
                        }
                    },"json");
                });
            }else if(obj.event==='edit'){
                layer.open({
                    type:1,
                    id:'editForm',
                    title:"修改商品信息",
                    area:['360px','400px'],
                    content:$("#goodsEmp"),
                    shade:0,
                })
                form.val('goodsForm',data);
            }else if(obj.event === 'sel'){
                table.render({
                    id:'table2',
                    // data:{"deptid":data.deptid},
                    // data:data.deptid,
                    elem: '#demo01',
                    url:'${pageContext.request.contextPath}/dept/sel',
                    cols: [[ //表头
                        {type: 'checkbox', fixed: 'left'}
                        ,{field: 'id', title: 'deptid',hide:true}
                        ,{field: 'staffid', title: '员工id'}
                        ,{field: 'staffname', title: '员工名称'}
                        ,{field: 'password', title: '密码',hide:true}
                        ,{field: 'idcard', title: '身份证',width:160,templet:function (c) {
                                if (c.idcard.length==18){
                                    var a=c.idcard.replace(/^(.{6})(?:\d+)(.{4})$/,"$1******$2");
                                    return a;
                                }
                            }}
                        ,{field: 'address', title: '地址'}
                        ,{field: 'phone', title: '手机号'}
                    ]]
                    ,page: true,//开启分页
                    limit:10,//设置每页条数
                    limits:[5,10,15,20]
                });
                table.reload('table2',{
                    where: { //设定异步数据接口的额外参数
                        deptid:data.deptid
                    }
                })
                layer.open({
                    type:1,
                    content:$("#dem"),
                    area:['1000px','500px'],
                    offset:'t'
                })

                form.val('deptForm', obj.data);
            }
        });
        //修改按钮的事件
        form.on('submit(update)',function(data){
            $.ajax({
                url:"${pageContext.request.contextPath}/dept/addOrUpdateDept",
                data: data.field,
                type:"post",
                dataType:"json",
                success: function(result){
                    if (result.code==1){//编号不重复
                    }else {//编号重复
                        message ="店铺已存在，请重新输入！"
                    }
                    layer.closeAll();
                    if(result.code===2000){
                        layer.msg(result.msg,{icon:1});
                        table.reload('test');
                    }else{
                        layer.msg(result.msg,{icon:5});
                        table.reload('test');
                    }
                }
            });
        })

        //头部工具栏
        table.on('toolbar(test)',function(obj) {
            var checkStatus=table.checkStatus(obj.config.id);
            var event=obj.event;
            if (event==='addFood'){
                layer.open({
                    type:1,
                    area:['350px','350px'],
                    content:$("#goodsEmp"),
                    title:'添加员工信息'
                })
            }else if(event==='sxin') {
                var checkStatus = table.checkStatus(obj.config.id);
                //获取被选中的数据
                var data = checkStatus.data;
                table.reload('test');
            }
        })

    })
</script>
</body>
</html>
